package AndroidDocSync.frontend;

import java.io.File;
import java.util.List;

import docsync.AndroidFolder;
import docsync.FolderInterface;
import docsync.GoogleDocSync;
import docsync.FolderSync.Action;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class showActionList extends ListActivity {

	public static final String LOG_TAG = "showDocList";

	List<Action> actionList;
	String[] listItems = { "item 1", "item 2 ", "list", "android", "item 3",
			"foobar", "bar", };

	// HashMap<String, String> xmlParsed = new HashMap<String, String>();
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.doclist);

//		ArrayList<String> listItems = new ArrayList<String>();

//		ProgressDialog pDialog = ProgressDialog.show(showDocList.this, "",
//				"Retrieving Google Doc Filelist...");

		Bundle listBundle = this.getIntent().getExtras();

		
/*		FolderInterface folder = new AndroidFolder(this.getDir("docsync",
				  Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE));
*/
		FolderInterface folder = new AndroidFolder(new File("/sdcard/cmpe207/"));

		GoogleDocSync sync = new GoogleDocSync(folder,
				listBundle.getString("user"), listBundle.getString("pwd"));

		Log.v(LOG_TAG, "GoogleDocSync object created");
		
		try {
			actionList = sync.synchronize();
		} 
		catch (Exception e)
		{ // TODO Auto-generated catch block
			Log.v(LOG_TAG, "exception thrown, going back to login screen");

			Intent i2 = new Intent(showActionList.this, login.class);
			i2.putExtra("log", "login failed, try again...");
			startActivity(i2);
			
			finish();
			return;
		}
		
		
//		pDialog.dismiss();
		ArrayAdapter<Action> arrayAdapter = new ArrayAdapter<Action>(this,
				android.R.layout.simple_list_item_1, actionList);
		setListAdapter(arrayAdapter);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Log.v(LOG_TAG, "id is - " + position);
		
		try {
			actionList.get(position).perform();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}

		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.doclistmenu, menu);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent i3 = new Intent(showActionList.this, showDocList.class);
		startActivity(i3);
		return true;
	}
		
}
